<script lang="ts" setup>
import { onBeforeMount, onMounted, reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { loginAPI, getAllUser } from "../request/apis/UsersApi";
import { storage } from "../utils/storage";
import { useRouter } from "vue-router";

const router = useRouter();
const userStore = storage("userToken");
const account = "";
const from = reactive({
  username: "",
  password: "",
});
const fromRuleRef = ref<FormInstance>();

const rules = reactive<FormRules>({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: '长度在6到16上', trigger: 'blur' },
  ],
});

onMounted(() => { });
onBeforeMount(() => {
  if (userStore.get() != null) {
    router.push('/index')
  }

});
const handleLogin = async (ruleFrom: FormInstance | undefined) => {
  if (!ruleFrom) return;
  ruleFrom.validate((valid, fields) => {
    if (valid) {
      loginAPI({ username: from.username, password: from.password }).then(
        (res) => {
          let { code, data } = res;
          userStore.set(data);
          router.push('/index')
        }
      );
    } else {
      return;
    }
  });
};
</script>

<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">登录</h2>
      <el-form :model="from" :rules="rules" ref="fromRuleRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="from.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="from.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin(fromRuleRef)">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
  padding: 20px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.login_vue {
  width: 100%;
  height: 100%;
  display: block;
  /*  background-image: url(../../assets/loginBackground.jpg); */
}

.login_vue_top {
  padding-top: 180px;
  width: 100%;
  text-align: center;
}

.login_view {
  display: block;
  text-align: center;
  border: 1px solid rgb(254, 254, 254);
  background: rgba(253, 253, 253, 0.856);
  border-radius: 5px;
  width: 450px;
  margin: auto;
}

.login_view_top {
  width: 100%;
  height: 100px;
  padding: 0px;
  border-radius: 5px 5px 0px 0px;
}

.login_but {
  margin-top: 15px;
  margin-bottom: 10px;
  margin-right: 70px;
}

.account_text,
.password_text,
.login_but {
  width: 75%;
  margin: auto;
}

.account_text {
  margin-bottom: 20px;
  margin-top: 20px;
}

.password_text {
  margin-bottom: 20px;
}

.login_view_bottom {
  width: 100%;
  height: 30px;
  margin-bottom: 10px;
  padding-left: 15%;
  text-align: left;
}

.Register_div {
  margin-right: 45%;
}
</style>
